layui.use(['form','layer'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    form.verify({
        stuname: function(value, item){ //value：表单的值、item：表单的DOM对象
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                return '用户名不能有特殊字符';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '用户名首尾不能出现下划线\'_\'';
            }
            if(/^\d+\d+\d$/.test(value)){
                return '用户名不能全为数字';
            }
        }
        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        ,pass: [
            /^[\S]{6,12}$/
            ,'密码必须6到12位，且不能出现空格'
        ]
        ,xsxjfh: function(value,item){
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                return '用户名不能有特殊字符';
            }
        }
        ,xssfz: function(value,item){
            if(!validId18(value)){
                return '身份证有误。';
            }
        }
    });

    form.on("select(xxList)",function(data){
        var xxId = data.value;
        $.ajax({
            url: "/getBjListOption",//请求地址
            type: "POST",//请求方式
            dataType: "JSON",//返回数据类型
            data: {xxId: xxId},//发送的参数
            success:function(msg){
                $('#bjxxId option').hide();
                ///var res = eval("("+msg+")");
                var netList = msg.list;
                var proHtml = '';
                $.each(netList, function (i, item) {
                    proHtml += '<option value="' + item.id +  '">' + item.name + '</option>';
                });
                $('#bjxxId').html(proHtml);
                form.render();
            },
            error:function(){
                //失败执行的方法
                alert("error");
            }
        })

    });

    form.on("submit(addStudent)",function(data){
        var arr = new Array();
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //data.field.sex=$("input:radio[name='sex']:checked").val();
        if (data.field.sex == undefined || data.field.sex==null) {
            top.layer.msg("请先选择性别！");
            return false;
        }
        // 实际使用时的提交信息
        $.post("/studentAdd", {
            name: $(".name").val(),
            sfz: $(".sfz").val(),
            xjfh: $(".xjfh").val(),
            sex: data.field.sex,
            xxxxId: data.field.xxxxId,
            bjxxId: data.field.bjxxId,
            xsId: $("#xsId").val()
        }, function (data) {
            if (data =="success") {
                top.layer.close(index);
                top.layer.msg("添加成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }else if (data == "error") {
                top.layer.msg("添加失败！");
            }else{
                top.layer.msg(data);
            }
        });
/*        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("添加成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);*/
        return false;
    });
    var powers=new Array("7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2");
    var parityBit=new Array("1","0","X","9","8","7","6","5","4","3","2");
    function validId18(_id){
        _id=_id+"";
        var _num=_id.substr(0,17);
        var _parityBit=_id.substr(17);
        var _power=0;
        var sex = "";
        for(var i=0;i< 17;i++){
            //校验每一位的合法性
            if(_num.charAt(i)<'0'||_num.charAt(i)>'9'){
                return false;
                break;
            }else{
                //加权
                _power+=parseInt(_num.charAt(i))*parseInt(powers[i]);
                //设置性别
                if(i==16&&parseInt(_num.charAt(i))%2==0){
                    sex="female";
                }else{
                    sex="male";
                }
            }
        }
        //取模
        var mod=parseInt(_power)%11;
        console.log("MOD:"+mod);
        if(parityBit[mod]==_parityBit){
            return true;
        }
        return false;
    }
    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})